import numpy as np
import matplotlib.pyplot as plt

# 生成随机数据
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)

# 绘制数据图
plt.scatter(x, y, s=10)
plt.xlabel('x')
plt.ylabel('y')
plt.show()

# 线性回归模型
from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(x, y)

# 预测
x_new = np.array([[0], [1]])
y_new = model.predict(x_new)

# 绘制预测图
plt.plot(x_new, y_new, c='red', linewidth=2)
plt.scatter(x, y, s=10)
plt.xlabel('x')
plt.ylabel('y')
plt.show()